//
//  NSString+Utils.m
//  划词显示
//
//  Created by 盛超 on 2018/8/16.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "NSString+DicUtils.h"

@implementation NSString (DicUtils)

#pragma mark - html文本处理

- (NSAttributedString *)dic_HtmlString
{
    NSData *data = [self dataUsingEncoding:NSUnicodeStringEncoding];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:data options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    return attrStr;
}

- (NSAttributedString *)dic_HtmlStringWithFontSize:(float)fontSize
{
    NSMutableAttributedString *mutableString = self.dic_HtmlString.mutableCopy;
    
    [mutableString enumerateAttribute:(NSString *)NSFontAttributeName inRange:NSMakeRange(0, mutableString.length) options:NSAttributedStringEnumerationReverse usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        UIFont *font = (UIFont *)value;
        UIFontDescriptor *ctfFont = font.fontDescriptor;
        NSString *postScriptName = ctfFont.postscriptName;
        
        if (font)
        {
            CGFloat defaultFontSize = 12.0;//html富文本转换后默认默认大小为12.0
            [mutableString removeAttribute:NSFontAttributeName range:range];
            
            CGFloat diff = (fontSize - defaultFontSize) > 0 ? (fontSize - defaultFontSize) : 0;
            UIFont *newFont = [UIFont systemFontOfSize:font.pointSize + diff];
            
            if ([postScriptName isEqualToString:@"TimesNewRomanPS-BoldMT"]) {
                newFont = [UIFont boldSystemFontOfSize:font.pointSize + diff];
            }
            else if ([postScriptName isEqualToString:@"TimesNewRomanPS-ItalicMT"])
            {
                newFont = [UIFont italicSystemFontOfSize:font.pointSize + diff];
            }
            else if ([postScriptName isEqualToString:@"TimesNewRomanPS-BoldItalicMT"])
            {
                newFont = [UIFont fontWithName:@"Helvetica-BoldOblique" size:font.pointSize + diff];
            }
            
            [mutableString addAttribute:NSFontAttributeName value:newFont range:range];
        }
    }];
    
    [mutableString enumerateAttribute:(NSString *)NSForegroundColorAttributeName inRange:NSMakeRange(0, mutableString.length) options:NSAttributedStringEnumerationLongestEffectiveRangeNotRequired usingBlock:^(id  _Nullable value, NSRange range, BOOL * _Nonnull stop) {
        
        CGColorRef color = (__bridge CGColorRef)(value);
        
        NSString *colorPrintString = [NSString stringWithFormat:@"%@",color];
        
        //想不到更好的比较方式，暂时这样处理
        if ([colorPrintString isEqualToString:@"kCGColorSpaceModelRGB 0 0 0 1 "]) {
            [mutableString removeAttribute:NSForegroundColorAttributeName range:range];
            
            [mutableString addAttribute:NSForegroundColorAttributeName
                                  value:[UIColor blackColor]
                                  range:range];
        }
    }];
    
    return mutableString;
}

@end
